﻿$(function () {
    var validata = new Validate();
    var url = new Url();
    var sms = new SMS();
    sms.BindSMSTmpl(); //绑定模板

    //修改时绑定页面
    var $Id = url.GetQueryString("id");
    sms.BindEditPage($Id);
    //发送短息
    $(".btnSend").bind("click", function () {
        var $GetUserId = $(".hdUserId").val();
        if ($GetUserId == null || $GetUserId == "") {
            $(".GetUserId").click();
            $(".errorUser").text("请选择用户！");
            return;
        }
        //var $GetPhone = $(".txtGetPhone").val();
        var $MessageContent = $(".txtMessageContent").val();
        //if ($GetPhone == null || $GetPhone == "") {
        //    $(".txtGetPhone").focus();
        //    $(".errorGetPhone").text("手机号不能为空！");
        //    return;
        //}
        //if (!validata.CheckPhone($GetPhone)) {
        //    $(".txtGetPhone").focus();
        //    $(".errorGetPhone").text("请输入正确的手机号！");
        //    return;
        //}
        var $GetUserNameList = $(".GetUserId").val();
        if ($Id == null) {
            //新增
            sms.Add($GetUserId,$GetUserNameList,  $MessageContent);
            //发送信息
            sms.SendSMS($GetUserId, $GetUserNameList, $MessageContent);
        }
        else {
            //修改
            sms.Edit($Id, $MessageContent);
        }
    });

    $(".txtGetPhone").change(function () {
        $(".errorGetPhone").text("");
    });
    //取消
    $(".btnCancle").bind("click", function () {
        var url = new Url();
        var cssid = url.GetQueryString("css");
        location.href = "SMSList.aspx?css=" + cssid;
    });
    //搜索
    $(".btnSearch").bind("click", function () {
        var $userName = $(".txtName").val();
        var $idCard = $(".txtIDCard").val();
        var $productType = $(".ddlProductType").val();
        var $startDate = $(".dtp_inputStartDate").val();
        var $endDate = $(".dtp_inputEndDate").val();
        var $custormerType = $("input[name=CustomerType]:checked").val();
        switch($custormerType)
        {
            case "1":
                //理财客户
                $.getJSON("../Handler/FinancialOrder.ashx", {
                    UserName: $userName,
                    IDCard: $idCard,
                    ProductType: $productType,
                    StartDate: $startDate, EndDate: $endDate, type: 20, _: Math.random()
                },
                    function (result) {
                        $(".userList").empty();
                        if (result.Status) {
                            $(result.Data).each(function (index, item) {
                                $("#UserTemplete").tmpl(item).appendTo($(".userList"));
                            });
                        }
                        else {
                            alert(result.Message);
                        }
                        $(".btnAdd").css("display", result.Status ? "inline" : "none");
                    }
                );
                break;
            case "2":
                //借款客户
                $.getJSON("../Handler/LoanProductOrderHandler.ashx", {
                    UserName: $userName,
                    IDCard: $idCard,
                    ProductType: $productType,
                    StartDate: $startDate, EndDate: $endDate, type: 17, _: Math.random()
                },
                    function (result) {
                        $(".userList").empty();
                        if (result.Status) {
                            $(result.Data).each(function (index, item) {
                                $("#UserTemplete").tmpl(item).appendTo($(".userList"));
                            });
                        }
                        else {
                            alert(result.Message);
                        }
                        $(".btnAdd").css("display", result.Status ? "inline" : "none");
                    }
                );
                break;
        }       
    });
    //添加
    $(".btnAdd").click(function () {
        var nameList = new Array();
        var phoneList = new Array();
        var idList = new Array();
        $(".userList tr").each(function () {
            if ($(this).find(":checkbox:checked").length > 0) {
                nameList.push($(this).find(".name").text());
                phoneList.push($(this).attr("data-phone"));
                idList.push($(this).find(".userId").text());
            }
        });
        $(".GetUserId").val(nameList.join(","));
        $(".txtGetPhone").val(phoneList.join(","));
        $(".hdUserId").val(idList.join(","));
        $(".close").click();
    });

    $(".GetUserId").bind("click", function () {
        $(".btnShow").click();
    });
    $(".ddlSMSTmpl").change(function () {
        var tmplId = $(".ddlSMSTmpl").val();
        if (tmplId != -1) {
            sms.BindTemplateId(tmplId);
        }
        else {
            $(".txtMessageContent").val("");
        }
    });

    //默认绑定产品列表
    var product = new Product();
    var productList = product.GetProductDDLByType(1);
    $(".ddlProductType").html(productList);

    //根据客户类型绑定产品列表
    $("input[name=CustomerType]").click(function () {        
        var $productType = $(this).val();
        productList = product.GetProductDDLByType($productType);
        $(".ddlProductType").html(productList);
    })

   


});

var SMS = function () { };

SMS.prototype = {
    BindEditPage: function ($Id) {
        //绑定页面数据
        if ($Id != null) {
            $.ajax({
                type: "post",
                url: "../Handler/SMSHandler.ashx",
                dataType: "json",
                data: { Id: $Id, type: 4 },
                cache: false,
                complete: function () {
                },
                success: function (data) {
                    $(".txtGetPhone").val(data.GetPhone);
                    $(".txtMessageContent").val(data.MessageContent);
                }
            });
        }
    },
    Add: function ($GetUserId,$userList,  $MessageContent) {
        //新增
        $.ajax({
            type: "post",
            url: "../Handler/SMSHandler.ashx",
            dataType: "json",
            data: { UserIdList: $GetUserId, UserList: $userList,  MessageContent: $MessageContent, type: 1 },
            complete: function () {
            },
            success: function (result) {
                if (result.Status) {
                    //alert(result.Message);
                    //location.href = "SMSList.aspx";
                }
                else {
                    alert("保存失败！");
                }
            }
        });
    },
    Edit: function ($Id, $GetPhone, $MessageContent) {
        //修改
        $.ajax({
            type: "post",
            url: "../Handler/SMSMSHandler.ashx",
            dataType: "json",
            data: { Id: $Id, GetPhone: "", MessageContent: $MessageContent, type: 2 },
            complete: function () {
            },
            success: function (result) {
                if (result.Status) {
                    alert(result.Message);
                    location.href = "SMSList.aspx";
                }
                else {
                    alert("保存失败！");
                }
            }
        });
    },
    BindSMSTmpl: function () {
        $.ajax({
            type: "post",
            url: "../Handler/SMSTmpl.ashx",
            dataType: "json",
            data: { type: 9 },
            cache: false,
            complete: function () {
            },
            success: function (data) {
                var str = '<option value="-1">请选择</option>';
                $(data).each(function (i, e) {
                    str += '<option value="' + e.TemplateId + '">' + e.TemplateName + '</option>';
                });
                $(".ddlSMSTmpl").html(str);
            }
        });
    },
    BindTemplateId: function ($Id) {
        $.ajax({
            type: "post",
            url: "../Handler/SMSTmpl.ashx",
            dataType: "json",
            data: { Id: $Id, type: 4 },
            cache: false,
            complete: function () {
            },
            success: function (result) {
                $(".txtMessageContent").val(result.Data.TemplateContent);
            }

        });
    },
    SendSMS: function ($GetUserId, $userList, $content) {
        $.ajax({
            type: "post",
            url: "../Handler/SMSHandler.ashx",
            dataType: "json",
            data: { UserIdList: $GetUserId, UserList: $userList, Content: $content, type: 11 },
            cache: false,
            complete: function () {
            },
            success: function (data) {
                if (data > 0) {
                    alert("发送成功！");
                    location.href = "SMSList.aspx";
                }
                else {
                    alert("发送失败！");
                }
            }
        });
    }
}


var Product = function () { };
Product.prototype = {
    GetProductDDLByType: function ($ProductType) {
        //根据类型获取简单下拉列表
        var result = "";
        $.ajax({
            type: "post",
            url: "../../../Handler/ProductHandler.ashx",
            dataType: "json",
            data: { type: 0, ProductType: $ProductType },
            async: false,
            cache: true,
            complete: function () {
            },
            success: function (data) {

                var str = '<option value="-1">请选择</option>';
                $(data).each(function (i, e) {
                    str += '<option value="' + e.ProductId + '">' + e.ProductName + '</option>';
                });
                result = str;
                console.log(result);
            }
        });
        return result;
    }
}